import { useEffect, useState } from "react";

export function debounce(callback: Function, duration = 1000) {
  let timer: NodeJS.Timeout;
  return <T>(param: T) => {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => callback(param), duration);
  };
}

export function useDebounceState<T>(state: T, duration = 1000) {
  const [debounceState, setDebounceState] = useState(state);

  useEffect(() => {
    const timer = setTimeout(() => {
      setDebounceState(state);
    }, duration);

    return () => clearTimeout(timer);
  }, [state]);

  return debounceState;
}

export function useArray<T>(array: T[]) {}
